// /home/lester/HDPU/kinematix/kinematix/kGUI.cs created with MonoDevelop
// User: lester at 1:03 18.06.2007
//

using System;
using IrrlichtNETCP;

namespace kinematix
{
	
	
	public delegate void GUIClick(kGUI sender);
	
	public class kGUI
	{
		
	
		IrrlichtDevice device;
		VideoDriver driver;
		kRenderer render;
		GUIEnvironment env;
		Dimension2D size;
		kPythonInterpreter python;
		
		public GUIClick OnStartClick;
		public GUIClick OnStopClick;		
		public GUIClick OnReloadClick;		
		
		GUIStaticText textbox1;
		
		public kGUI(kRenderer _render, kPythonInterpreter _python)
		{
			python = _python;
			render = _render;
			device = _render.Device;
			driver = device.VideoDriver;
			env = device.GUIEnvironment;
			
			env.Skin.SetColor(GuiDefaultColor.ButtonText, Color.White);
			
		}
		
		
		/*
		 * Create buttons Start, Stop, Reload and the info box 
		 */
		public void SetupBasicUI()
		{
	
			int h = driver.ViewPort.Height;
			int w = driver.ViewPort.Width;
			GUIImage im1 = env.AddImage(new Rect(16,h-64,48,h-32),null,-1,"reload");
			im1.Image = driver.GetTexture("reload.png");
			im1.UseAlphaChannel = true;
			
			GUIButton button1 = env.AddButton(new Rect(0,h-32,64,h), null, (int)GuiElements.ReloadButton, "Reload"); 
			//button1.UseAlphaChannel = true;

			GUIImage im2 = env.AddImage(new Rect(80,h-64,112,h-32),null,-1,"start");
			im2.Image = driver.GetTexture("start.png");
			im2.UseAlphaChannel = true;
			
			GUIButton button2 = env.AddButton(new Rect(64,h-32,128,h), null, (int)GuiElements.StartButton, "Start"); 
			//button2.UseAlphaChannel = true;

			GUIImage im3 = env.AddImage(new Rect(144,h-64,176,h-32),null,-1,"stop");
			im3.Image = driver.GetTexture("stop.png");
			im3.UseAlphaChannel = true;
			
			GUIButton button3 = env.AddButton(new Rect(128,h-32,192,h), null, (int)GuiElements.StopButton, "Stop"); 
			//button3.UseAlphaChannel = true;

			GUIImage im4 = env.AddImage(new Rect(w-232,h-64,w-168,h-32),null,-1,"help");
			im4.Image = driver.GetTexture("help.png");
			im4.UseAlphaChannel = true;
			
			textbox1 = env.AddStaticText("", new Rect(w-400,h-32, w, h), true, true, null, -1, true);
			
			
			
			
		}
		
		/*
		 * Show message box function, used in scripts 
		 */
		public void ShowMessageBox(string text)
		{
			device.SceneManager.ActiveCamera.InputReceiverEnabled = false;
			device.CursorControl.Visible = true;
			env.AddMessageBox("Увага!",text, false, MessageBoxFlag.OK, null, -1);
		}
		/*
		 * This needs additional hacking of the irrlicht engine, since it doesn't provide OnResize event 
		 */
		
		public void Resize(Dimension2D newsize)
		{
			size = newsize;
		}
		
		public void SetInfoText(string s)
		{
			textbox1.Text = s;
		}
		
		public bool OnEvent(IrrlichtNETCP.Event ev)
		{
			switch (ev.GUIEvent)
			{
				case GUIEventType.ButtonClicked:
					if (ev.Caller.ID == (int)GuiElements.StartButton)					
					{
					if (OnStartClick != null)
					{
						OnStartClick(this);
					}
					render.World.Run = true;
				} else if (ev.Caller.ID == (int)GuiElements.ReloadButton)
					{
					if (OnReloadClick != null)
					{
						OnReloadClick(this);
					}
				python.Reload();
				} else if (ev.Caller.ID == (int)GuiElements.StopButton)
					{
					if (OnStopClick != null)
					{
						OnStopClick(this);
					}
					render.World.Run = false;
				} return false;
					break;
					
				default:
					return false;
			}
			return false;
		}
		
		public IrrlichtNETCP.GUIEnvironment Gui
		{
			get{
				return env;
			}
		}
		
	}
	
	public enum GuiElements
	{
		ReloadButton,
		StartButton,
		StopButton
	}			                                  
}
